﻿using System;
using System.Collections.Generic;
using FireStarterModeller.UI.LookupLists;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Base;
using Habanero.Faces.Base;

namespace FireStarterModeller.UI
{
    public interface IFireStarterFormsController
    {
        IControlFactory ControlFactory { get; }
        ILookupListControl CreateNoneLookupTypeControl();
        ILookupListControl CreateSimpleLookupTypeControl();
//        ILookupListControl CreateDatabaseLookupTypeControl();
        ILookupListControl CreateBusinessObjectLookupTypeControl();
        IControlHabanero CreateXmlEditor();
        IFormHabanero CreateShowErrorsForm(IList<IBOError> errors, string formTitle, string headingText, string descriptionText);
        IFormHabanero MainProgramForm { get; set; }
    }

    public class FireStarterFormsController : IFireStarterFormsController
    {
        public IControlFactory ControlFactory { get; private set; }

        public FireStarterFormsController(IControlFactory controlFactory)
        {
            ControlFactory = controlFactory;
        }

        public ILookupListControl CreateNoneLookupTypeControl()
        {
            return new NoneLookupListControl(ControlFactory);
        }

        public ILookupListControl CreateSimpleLookupTypeControl()
        {
            return new SimpleLookupListControl(ControlFactory);
        }

        public ILookupListControl CreateBusinessObjectLookupTypeControl()
        {
            return new BusinessObjectLookupListControl(ControlFactory);
        }

        public virtual IControlHabanero CreateXmlEditor()
        {
            return new ScintillaWinXmlEditor();
        }

        //public void ShowErrors(IList<IBOError> errors, string formTitle, string headingText, string descriptionText)
        //{
        //    IFormHabanero form = CreateShowErrorsForm(errors, headingText, descriptionText, formTitle);
        //    form.Show();
        //}

/*
        public IFormHabanero CreateShowErrorsForm(IList<IBOError> errors, string formTitle, string headingText, string descriptionText,EventHandler revalidateDelegate)
        {
            ErrorViewerControl errorViewerControl = new ErrorViewerControl {ErrorList = errors};
            IUserControlHabanero mainPanel = this.ControlFactory.CreateUserControl();
            IPanel panel = this.ControlFactory.CreatePanel();
            IButtonGroupControl buttonGroupControl = this.ControlFactory.CreateButtonGroupControl();
            buttonGroupControl.AddButton("OK",(sender,args) => ((IFormHabanero) sender).DialogResult = DialogResult.OK);
            buttonGroupControl.AddButton("Cancel", (sender, args) => ((IFormHabanero)sender).DialogResult = DialogResult.Cancel);
            buttonGroupControl.AddButton("ReValidate",revalidateDelegate);
            ColumnLayoutManager columnLayoutManager = new ColumnLayoutManager(panel, this.ControlFactory) {ColumnCount = 1};
            columnLayoutManager.AddControl(this.ControlFactory.CreateLabel(headingText, true));
            columnLayoutManager.AddControl(this.ControlFactory.CreateLabel(descriptionText, true));
            panel.Height = 50;
            BorderLayoutManager layoutManager = this.ControlFactory.CreateBorderLayoutManager(mainPanel);
            layoutManager.AddControl(panel, BorderLayoutManager.Position.North);
            layoutManager.AddControl(errorViewerControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(buttonGroupControl, BorderLayoutManager.Position.South);
            IFormHabanero formHabanero = this.ControlFactory.CreateForm();
            formHabanero.Width = 800;
            formHabanero.Height = 600;
            BorderLayoutManager formLayoutManager = this.ControlFactory.CreateBorderLayoutManager(formHabanero);
            formLayoutManager.AddControl(mainPanel, BorderLayoutManager.Position.Centre);
            return formHabanero;
        }
*/

        public IFormHabanero CreateShowErrorsForm(IList<IBOError> errors, string formTitle, string headingText, string descriptionText)
        {
            ErrorViewerControl errorViewerControl = new ErrorViewerControl { ErrorList = errors };
            IUserControlHabanero mainPanel = this.ControlFactory.CreateUserControl();
            IPanel panel = this.ControlFactory.CreatePanel();
            ColumnLayoutManager columnLayoutManager = new ColumnLayoutManager(panel, this.ControlFactory) { ColumnCount = 1 };
            columnLayoutManager.AddControl(this.ControlFactory.CreateLabel(headingText, true));
            columnLayoutManager.AddControl(this.ControlFactory.CreateLabel(descriptionText, true));
            panel.Height = 50;
            BorderLayoutManager layoutManager = this.ControlFactory.CreateBorderLayoutManager(mainPanel);
            layoutManager.AddControl(panel, BorderLayoutManager.Position.North);
            layoutManager.AddControl(errorViewerControl, BorderLayoutManager.Position.Centre);
            mainPanel.Width = 800;
            mainPanel.Height = 600;
            return this.ControlFactory.CreateOKCancelDialogFactory().CreateOKCancelForm(mainPanel, formTitle);
        }

        public IFormHabanero MainProgramForm{ get; set; }
        
    }
}
